package demos;

import java.util.concurrent.TimeUnit;

class Source {
    public synchronized void sayHello() {
        try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) {e.printStackTrace();}
        System.out.println(Thread.currentThread().getName() + "****Hello");
    }

    public synchronized void sayNo() {
        System.out.println(Thread.currentThread().getName() + "****NoNo");
    }

    public synchronized static void sayClass() {
        try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) {e.printStackTrace();}
        System.out.println(Thread.currentThread().getName() + "****Class");
    }

    public static void sayClassNormal() {
        System.out.println(Thread.currentThread().getName() + "****ClassNormal");
    }

    public void sayNormal() {
        System.out.println(Thread.currentThread().getName() + "****Normal");
    }
}

public class SynchronizedDemo {
    static Integer obj = 0;
    private static void method() {
        synchronized (obj) {
            obj += 1;
        }
    }
    public static void main(String[] args) {
        Source src = new Source();

//        new Thread(Source::sayClass, "A").start();
        // new Thread(src::sayNormal, "A").start();
         new Thread(src::sayHello, "A").start();
        
        try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) {e.printStackTrace();}

//        new Thread(Source::sayClassNormal, "B").start();
        new Thread(src::sayNormal, "B").start();
    }
}

/*
* 类锁：锁的是类的所有带锁资源
* 对象锁：锁的是实例对象的所有带锁资源
* synchronized 修饰普通方法 => synchronized(this)
* synchronized 修饰static方法 => synchronized(Class)
*/